import { Component, Host } from "@angular/core";
import { AppComponent } from "../../../app/app.component";
import { GovernanceReportsService } from "./governanceReports.service";
import { Router, ActivatedRoute } from "@angular/router";

@Component({
    templateUrl: 'governanceReports.component.html'
})
export class GovernanceReportsComponent {
    public term: string;
    public searchResults: any[];
    public found: boolean;

    constructor(@Host() private rootComponent: AppComponent,
    private governanceService: GovernanceReportsService,
    private router: Router,
    private route: ActivatedRoute){}

    ngOnInit(){
        this.rootComponent.finishLoading();
    }

    public search() {
        this.governanceService.getCompanies(this.term).subscribe(data=>{
            this.searchResults = data;            
            this.found = this.searchResults && this.searchResults.length > 0;
        });      
    }

    public focusOut(event: any) {
        //not to lose focus before redirecting to the desired criteria
        this.found = event.relatedTarget != null && event.relatedTarget.offsetParent != null && event.relatedTarget.offsetParent.id == 'searchGroup';
    }
    
    public navigateToCompany(id: string, name: string) {
        this.found = false;      
        this.router.navigate(['company', id, name], { relativeTo: this.route });
    }

}